from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from .models import Roles, RoleResource, AdminUser, Resources, \
    Workflow, WorkflowPermission, Task, Department, AuditRecord, Position


class ResourcesSer(ModelSerializer):
    class Meta:
        model = Resources
        fields = '__all__'


class AdminUserSer(ModelSerializer):
    depart = serializers.SerializerMethodField()
    position = serializers.SerializerMethodField()
    roles = serializers.SerializerMethodField()

    def get_depart(self, obj):
        return obj.dep.name

    def get_position(self, obj):
        return obj.pos.name

    def get_roles(self, obj):
        return obj.role.role_name

    class Meta:
        model = AdminUser
        fields = '__all__'


class RolesSer(ModelSerializer):
    class Meta:
        model = Roles
        fields = '__all__'


class RoleResourceSer(ModelSerializer):
    roles = serializers.SerializerMethodField()
    resources = serializers.SerializerMethodField()

    def get_roles(self, obj):
        return obj.role.role_name

    def get_resources(self, obj):
        return obj.resource.resource_name

    class Meta:
        model = RoleResource
        fields = '__all__'


class WorkflowSer(ModelSerializer):
    class Meta:
        model = Workflow
        fields = '__all__'


class TaskSer(ModelSerializer):
    class Meta:
        model = Task
        fields = '__all__'


# class AuditRecordSer(ModelSerializer):
#     class Meta:
#         model = AuditRecord
#         fields = '__all__'


class AuditRecordSer(ModelSerializer):
    class Meta:
        model = AuditRecord
        fields = ('task', 'audit_user', 'audit_status', 'audit_time')


class PositionSer(ModelSerializer):
    class Meta:
        model = Position
        fields = '__all__'

